package io.bdmc.modules.bss.controller;

import com.alibaba.fastjson2.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.bdmc.common.annotation.SysLogger;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.DeviceDevice;
import io.bdmc.modules.bss.entity.DeviceMachineOpm;
import io.bdmc.modules.bss.model.DeviceMachineOpmParam;
import io.bdmc.modules.bss.service.IDeviceDeviceService;
import io.bdmc.modules.bss.service.IDeviceMachineOpmService;
import io.bdmc.modules.sys.controller.AbstractController;
import io.bdmc.core.msp2000.utils.OpmDeviceUtil;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 黄露
 * @since 2021-05-28
 */

// "", description = "")
@RestController
@RequestMapping("/bss/device/opm")
public class DeviceMachineOpmController extends AbstractController {

	@Autowired
	IDeviceMachineOpmService _opmService;

	@Autowired
	OpmDeviceUtil opmDeviceUtil;

	@Autowired
	IDeviceDeviceService deviceService;

	@GetMapping("/list")
	// "分页查询", notes = "分页查询")
	@RequiresPermissions("bss:device:opm:list")
	public R list(DeviceMachineOpmParam params) {
		MyPage<DeviceMachineOpm> myPage = new MyPage<DeviceMachineOpm>(params.getCurrent(), params.getSize());
		myPage = _opmService.mySelectPage(myPage);
		return R.ok().putPageData(myPage.getRecords(), myPage.getTotal());

	}

	// @SysLogger("设置OPM参数")
	@PostMapping("/set")
	// "设置OPM参数", notes = "设置OPM参数")
	public R set(@RequestBody JSONObject jsonObject) {
		final String wavelength = jsonObject.getString("wavelength");
		final String deviceSn = jsonObject.getString("deviceSn");
		final Integer channelNum = jsonObject.getInteger("channelNum");
		DeviceDevice dd = deviceService.getDeviceBySn(deviceSn);
		DeviceDevice parent = deviceService.getById(dd.getParentId());
		boolean setResult = false;
		if (StringUtils.isNotEmpty(wavelength)) {

			setResult = opmDeviceUtil.setOpmWaveLength(parent.getIpaddr(), parent.getDevicePort(), deviceSn,
					wavelength);
		}
		if (channelNum != null && channelNum > 0) {
			setResult = opmDeviceUtil.setOpmChannelNumber(parent.getIpaddr(), parent.getDevicePort(), deviceSn,
					channelNum);
		}

		return R.trueOrFalse(setResult);
	}

	@SysLogger("修改")
	@PostMapping("/update")
	// "修改", notes = "修改")
	@RequiresPermissions("bss:device:opm:update")
	public R update(@RequestBody DeviceMachineOpm devicemachineopm) {
		Boolean b = devicemachineopm.updateById();
		return R.trueOrFalse(b);
	}

}
